package com.ldk.base.framework.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.beanutils.BeanMap;

import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.PropertyUtils;

public class BeanUtil {
	/**
	 * 对象转化为Map
	 * @param obj
	 * @return
	 */
	public static Map<String, Object> toMap(Object obj) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (obj == null) {
			 
			return map;
		}
		
		BeanMap beanMap = new BeanMap(obj);
		Iterator<String> it = beanMap.keyIterator();
		while (it.hasNext()) {
			String name = it.next();
			Object value = beanMap.get(name);
			if (value != null && !name.equals("class")) {
				map.put(name, value);
			}
		}
		return map;
	}
	/**
	 * 对象转化为Map1
	 * @param obj
	 * @return
	 */
	public static Map<String, Object> toMapLoc(Object obj) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (obj == null) {

			return map;
		}

		BeanMap beanMap = new BeanMap(obj);
		Iterator<String> it = beanMap.keyIterator();
		while (it.hasNext()) {
			String name = it.next();
			Object value = beanMap.get(name);
			if (value != null && !name.equals("class")) {
				String a=toUpperCaseFirstOne(name);
				map.put(a, value);
			}
		}
		return map;
	}
	/**
	 * 对象转化为Map
	 * @param objs
	 * @return
	 */
	public static Map<String, Object> toMap(Object... objs) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (Object object : objs) {
			if (object != null) {
				map.putAll(toMap(object));
			}
		}
		return map;
	}
	
	/**
	 * 对象转化为Map
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> toMapByFastjson(Object obj) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (obj == null) {
			return map;
		}
		try{
			String json = JSON.toJSONString(obj);
			map = JSON.parseObject(json, map.getClass());
		}
		catch(Exception e){
			
		}
		return map;
	}
	/**获取接口的泛型类型，如果不存在则返回null
	 * @author xiangwl
	 */
	public static Class<?> getGenericClass(Class<?> clazz) {
		Type t = clazz.getGenericSuperclass();
		if (t instanceof ParameterizedType) {
			Type[] p = ((ParameterizedType) t).getActualTypeArguments();
			return ((Class<?>) p[0]);
		}
		return null;
	}
	
	/** 
	 * 根据属性名获取属性值 
	 * */  
   public static Object getFieldValueByName(String fieldName, Object o) {  
       try {    
           String firstLetter = fieldName.substring(0, 1).toUpperCase();    
           String getter = "get" + firstLetter + fieldName.substring(1);    
           Method method = o.getClass().getMethod(getter, new Class[] {});    
           Object value = method.invoke(o, new Object[] {});    
           return value;    
       } catch (Exception e) {     
           return null;    
       }    
   }
   
   /**
	 * 对象转化为整形，默认返回0
	 * @param value
	 * @param defaultValue
	 * @return
	 */
	public static int parseInt(Object value, int defaultValue){
		String strValue = String.valueOf(value);
		try{
			if(!RegexUtil.isInt(strValue)){
				strValue = String.valueOf(defaultValue);
			}
			else{
				strValue = String.valueOf(value);
			}
		}catch(Exception e){
			strValue = String.valueOf(defaultValue);
		}
		if(strValue == null || strValue == "" ||  strValue.trim().length() < 1)
			strValue = String.valueOf(defaultValue);
		return Integer.valueOf(strValue);
		
	}


	public static <T> T copy(Object obj, final Class<T> clz) {
		try {
			//if(Objects.(obj)) {
				Class<?> c = obj.getClass();
				Object newObj = c.newInstance();
				PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(c);
				for(PropertyDescriptor propertyDescriptor:propertyDescriptors) {
					String name = propertyDescriptor.getName();
					name = toUpperCaseFirstOne(name);
					Method get = c.getMethod("get" + name);
					Object invoke = get.invoke(obj);
					Method set = c.getMethod("set" + name, propertyDescriptor.getPropertyType());
					set.invoke(newObj,invoke);
				}
				//String jsonStr = toJSONStr(newObj);
				//if(StringUtils.isEmpty(jsonStr)) {
				//    return null;
				//}else {
				//    return (T)jsonStr.toCharArray();
				//}
				return get(clz, newObj);
			//}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static <T> T get(Class<T> clz,Object o) {
		if(clz.isInstance(o)){
			return clz.cast(o);
		}
		return null;
	}
	/**
	 * @Description: 首字母转小写
	 * @Author: 王晨阳
	 * @LastUpdater: 王晨阳
	 * @Date: 2020/3/14-12:35
	 */
	public static String toLowerCaseFirstOne(String s){
		if(Character.isLowerCase(s.charAt(0))) {
			return s;
		}else {
			return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
		}
	}

	/**
	 * @Description: 首字母转大写
	 * @Author: 王晨阳
	 * @LastUpdater: 王晨阳
	 * @Date: 2020/3/14-12:35
	 */
	public static String toUpperCaseFirstOne(String s){
		if(Character.isUpperCase(s.charAt(0))) {
			return s;
		}else {
			return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
		}
	}

}
